//
//  DetailSubViewController.m
//  WGFGlobalHouses
//
//  Created by waiguofang on 15/6/15.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "DetailSubViewController.h"
#import "HouseDetailCell1.h"
#import "HouseDetailCell2.h"
#import "NearbyCell2.h"
#import "HouseDetailDataManage.h"
#import "HouseTypeData.h"
#import "UserModal.h"
#import "SingletonData.h"
#import "NodataCell.h"
#import "DistanceCell.h"
#import "FangYuanCell.h"
#import "FangXingCell.h"
#import "HouseIntroducedCell.h"
#import "disDescModel.h"
#import "NearbyCellData.h"
#import "TxtReadVc.h"

#import "config.h"



@interface DetailSubViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>
@property(nonatomic,strong)UITableView *tableView1;
@property(nonatomic,strong)UITableView *tableView2;
@property (strong, nonatomic) HouseDetailDataManage *dataManage;
@property (strong, nonatomic) NodataCell *nodataCell;

@property (nonatomic,strong)UITableViewCell *fxcel,*fZcell;

@property(nonatomic,assign) NSInteger flag;//flag = 0 tableview1,falg = 2 tableview2;


@end

@implementation DetailSubViewController
@synthesize nodataCell;

//- (void)viewDidLoad {
//    [super viewDidLoad];
//    
//    [self setEdgesForExtendedLayout:UIRectEdgeNone];
//    [self initTableView];
//    
//}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setEdgesForExtendedLayout:UIRectEdgeNone];
//    [self initComponents];
    [self initTableView];
}

- (void)addRefreshView:(UITableView *)tableView{
    if (self.pullFreshView == nil) {
        self.pullFreshView = [[NLPullDownRefreshView alloc]initWithFrame:CGRectMake(0, -38.f, self.view.frame.size.width, 38.f)];
        self.pullFreshView.backgroundColor = [UIColor whiteColor];
    }
    
    if (!self.pullFreshView.superview) {
        [self.pullFreshView setupWithOwner:tableView delegate:(id<NLPullDownRefreshViewDelegate>)self.mainTableViewController];
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self.pullFreshView scrollViewWillBeginDragging:scrollView];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self.pullFreshView scrollViewDidScroll:scrollView];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    [self.pullFreshView scrollViewDidEndDragging:scrollView willDecelerate:decelerate];
}


-(void)initTableView
{
    
    
    
    UITableView *tableView2 = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, UIScreenWidth, UIScreenHeight)];
    tableView2.delegate = self;
    tableView2.separatorStyle = UITableViewCellSelectionStyleNone;
    tableView2.dataSource = self;
    tableView2.separatorStyle = UITableViewCellSelectionStyleNone;
    tableView2.allowsSelection = NO;
    [tableView2 setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:tableView2];
    self.tableView2 = tableView2;
    [self.tableView2 reloadData];
    
    
}



#pragma mark 把距离表述，筛选到最多只有三条（中国城，学区房，商业中心）
-(void)selectDisdesc
{
    NSInteger i = 0,j = 0,k = 0;
    NSMutableArray *arrM = [NSMutableArray arrayWithArray:self.model.disDescArr];
    if (arrM.count == 0)
    {
        self.model.disDescArr = nil;
    }
    else
    {
        for (disDescModel *mod in arrM)
        {
            
            if ([mod.lTypeName isEqual:@"中国城房"])
            {
                i ++;
                if (i > 1)
                {
                    [self.model.disDescArr removeObject:mod];
                }
            }else if([mod.lTypeName isEqual:@"学区房"])
            {
                j ++;
                if (j > 1)
                {
                    [self.model.disDescArr removeObject:mod];
                }
                
            }else
            {
                k ++;
                if (k > 1)
                {
                    [self.model.disDescArr removeObject:mod];
                }
                
            }
            
        }
        
    }
    
}


#pragma mark 获取数据
-(void)freshData
{
    
    [nodataCell setState:nodataCellStateLoading];
    
    __weak typeof(self) bself = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        
        
        ResulteMod *mod = [self.dataManage refreshDataWithConsumerId:[NSString stringWithFormat:@"%ld",(long)[UserModal shareUserModel].userId] WithPremiseshousingId:self.premiseshousingId];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            if (mod.resltState == ResultNetError) {//网络出错
                
                [bself.nodataCell setState:nodataCellStateNetError];
                
            }else if(mod.resltState == ResultFail){//服务器返回的code出错
                
                [bself.nodataCell setCodeErrorTip:mod.resultCode];
                
            }else{//返回正常
                
                self.model = mod.dataMod;
                
                if(self.model == nil){//没数据
                    
                    [bself.nodataCell setState:nodataCellStateNodata];
                }
            }
            
            [bself.tableView2 reloadData];
            
            
        });
    });
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   
            return  4;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0)
            {
                return [HouseIntroducedCell cellWithTableView:tableView WithDetail:self.model.detail WithDetailEnglish:self.model.detailEnglish];
            }else if(indexPath.row == 1)
            {
                [self selectDisdesc];
                
                return [DistanceCell cellWithTable:tableView withDisDescArr:self.model.disDescArr];
                
            }else if (indexPath.row == 2)
            {
                
                return [FangXingCell cellWithTableView:tableView WithFangXingPicsArr:self.model.fangxingPicsArr];
            }
            else
            {
                return [FangYuanCell cellWithTableView:tableView WithFangXingPicsArr:self.model.fangyuanPicsArr];
            }
    
   
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
            if (indexPath.row == 1)
            {
                return [DistanceCell getHeightWithTable:tableView withDisDescArr:self.model.disDescArr];
                
            }
            else if(indexPath.row == 2)
            {
                
                return [FangXingCell getHeightWithTableView:tableView WithFangXingPicsArr:self.model.fangxingPicsArr];
            }
            else if(indexPath.row == 3)
            {
                
                return [FangYuanCell getHeightWithTableView:tableView WithFangYuanPicsArr:self.model.fangyuanPicsArr];
                
            }
            else
            {
                
                return [HouseIntroducedCell getHeightWithWithDetail:self.model.detail WithDetailEnglish:self.model.detailEnglish WithTableView:tableView];
            }
    
}

@end
